{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "model_versioning.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "K1iUeucqZs5s",
        "colab_type": "text"
      },
      "source": [
        "## Model Versioning Design Pattern\n",
        "\n",
        "In the Model Versioning design pattern, backward compatibility is achieved by deploying a changed model as a microservice with a different REST endpoint. This is a necessary prerequisite for many of the other patterns discussed in this chapter."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TlJZejojmM1Y",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import json\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import xgboost as xgb\n",
        "import tensorflow as tf\n",
        "\n",
        "from sklearn.utils import shuffle\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import accuracy_score\n",
        "\n",
        "from google.cloud import bigquery"
      ],
      "execution_count": 27,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pjF62lGb5wRf",
        "colab_type": "text"
      },
      "source": [
        "## Download and preprocess data\n",
        "\n",
        "You'll need to authenticate to your Google Cloud to run the BigQuery query below."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dJfrYrQDz6LC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import auth\n",
        "auth.authenticate_user()"
      ],
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9qklhTaJbLVC",
        "colab_type": "text"
      },
      "source": [
        "In the following cell, replace `your-cloud-project` with the name of your GCP project."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ps6pJb0JzeTq",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Note: this query may take a few minutes to run\n",
        "%%bigquery df --project your-cloud-project\n",
        "SELECT\n",
        "  arr_delay,\n",
        "  carrier,\n",
        "  origin,\n",
        "  dest,\n",
        "  dep_delay,\n",
        "  taxi_out,\n",
        "  distance\n",
        "FROM\n",
        "  `cloud-training-demos.flights.tzcorr`\n",
        "WHERE\n",
        "  extract(year from fl_date) = 2015\n",
        "ORDER BY fl_date ASC\n",
        "LIMIT 300000"
      ],
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JGhpygtJ2xvU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "df = df.dropna()\n",
        "df = shuffle(df, random_state=2)"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "R5B5w2PS0FJc",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 197
        },
        "outputId": "fb1bb459-b85c-4628-e203-15dbf0e4d18e"
      },
      "source": [
        "df.head()"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>arr_delay</th>\n",
              "      <th>carrier</th>\n",
              "      <th>origin</th>\n",
              "      <th>dest</th>\n",
              "      <th>dep_delay</th>\n",
              "      <th>taxi_out</th>\n",
              "      <th>distance</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>288655</th>\n",
              "      <td>4.0</td>\n",
              "      <td>US</td>\n",
              "      <td>PHX</td>\n",
              "      <td>SJC</td>\n",
              "      <td>-1.0</td>\n",
              "      <td>16.0</td>\n",
              "      <td>621.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>45636</th>\n",
              "      <td>-15.0</td>\n",
              "      <td>WN</td>\n",
              "      <td>OKC</td>\n",
              "      <td>STL</td>\n",
              "      <td>1.0</td>\n",
              "      <td>12.0</td>\n",
              "      <td>462.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>247620</th>\n",
              "      <td>-1.0</td>\n",
              "      <td>WN</td>\n",
              "      <td>OAK</td>\n",
              "      <td>LAX</td>\n",
              "      <td>7.0</td>\n",
              "      <td>7.0</td>\n",
              "      <td>337.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>294786</th>\n",
              "      <td>-21.0</td>\n",
              "      <td>UA</td>\n",
              "      <td>PDX</td>\n",
              "      <td>DEN</td>\n",
              "      <td>-8.0</td>\n",
              "      <td>12.0</td>\n",
              "      <td>991.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>282690</th>\n",
              "      <td>-21.0</td>\n",
              "      <td>US</td>\n",
              "      <td>MCI</td>\n",
              "      <td>PHX</td>\n",
              "      <td>-4.0</td>\n",
              "      <td>10.0</td>\n",
              "      <td>1044.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "        arr_delay carrier origin dest  dep_delay  taxi_out  distance\n",
              "288655        4.0      US    PHX  SJC       -1.0      16.0     621.0\n",
              "45636       -15.0      WN    OKC  STL        1.0      12.0     462.0\n",
              "247620       -1.0      WN    OAK  LAX        7.0       7.0     337.0\n",
              "294786      -21.0      UA    PDX  DEN       -8.0      12.0     991.0\n",
              "282690      -21.0      US    MCI  PHX       -4.0      10.0    1044.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UA8q3VRe1cNf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Only include origins and destinations that occur frequently in the dataset\n",
        "df = df[df['origin'].map(df['origin'].value_counts()) > 500]\n",
        "df = df[df['dest'].map(df['dest'].value_counts()) > 500]"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cPba5pvq2F5N",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "df = pd.get_dummies(df, columns=['carrier', 'origin', 'dest'])"
      ],
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tMHO0Tvg356i",
        "colab_type": "text"
      },
      "source": [
        "## Model version #1: predict whether or not the flight is > 30 min delayed"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ldq9F_U239yY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Create a boolean column to indicate whether flight was > 30 mins delayed\n",
        "df.loc[df['arr_delay'] >= 30, 'arr_delay_bool'] = 1\n",
        "df.loc[df['arr_delay'] < 30, 'arr_delay_bool'] = 0"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KnfMOA9v394u",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 70
        },
        "outputId": "0a767122-bb25-470e-d841-f7d172e38de4"
      },
      "source": [
        "df['arr_delay_bool'].value_counts()"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.0    195781\n",
              "1.0     33962\n",
              "Name: arr_delay_bool, dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "Bg3d38zV6wNZ",
        "colab": {}
      },
      "source": [
        "classify_model_labels = df['arr_delay_bool']\n",
        "classify_model_data = df.drop(columns=['arr_delay', 'arr_delay_bool'])"
      ],
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "Yk3NnfL26wNe",
        "colab": {}
      },
      "source": [
        "x,y = classify_model_data,classify_model_labels\n",
        "x_train,x_test,y_train,y_test = train_test_split(x,y)"
      ],
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "Z516D5m56wNg",
        "colab": {}
      },
      "source": [
        "model = xgb.XGBRegressor(\n",
        "    objective='reg:logistic'\n",
        ")"
      ],
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "YqISLRrg6wNj",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 141
        },
        "outputId": "05545a82-4986-467e-d195-8afbc39ca868"
      },
      "source": [
        "# Given the dataset size, this may take 1-2 minutes to run\n",
        "model.fit(x_train, y_train)"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n",
              "             colsample_bynode=1, colsample_bytree=1, gamma=0,\n",
              "             importance_type='gain', learning_rate=0.1, max_delta_step=0,\n",
              "             max_depth=3, min_child_weight=1, missing=None, n_estimators=100,\n",
              "             n_jobs=1, nthread=None, objective='reg:logistic', random_state=0,\n",
              "             reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,\n",
              "             silent=None, subsample=1, verbosity=1)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "cQ72yEUx6wNn",
        "colab": {}
      },
      "source": [
        "y_pred = model.predict(x_test)"
      ],
      "execution_count": 14,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QoT6dmYq7jK3",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "434f0d65-40ce-40b6-eb23-e646d239a55e"
      },
      "source": [
        "acc = accuracy_score(y_test, np.round(y_pred))\n",
        "print(acc)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.9651438122431925\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DCoY84hy9pGo",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Save the model\n",
        "model.save_model('model.bst')"
      ],
      "execution_count": 16,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DEl2SgUM7suS",
        "colab_type": "text"
      },
      "source": [
        "### Deploying classification model to AI Platform\n",
        "\n",
        "Replace `your-cloud-project` below with the name of your cloud project."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a2nY5CqO9JG4",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "d886376c-c156-4a36-9a4f-dc36a265e881"
      },
      "source": [
        "# Set your cloud project\n",
        "PROJECT = 'your-cloud-project'\n",
        "!gcloud config set project $PROJECT"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Updated property [core/project].\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9s7fW_Hq-V81",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "BUCKET = PROJECT + '_flight_model_bucket'"
      ],
      "execution_count": 21,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-PaQgaS5-ab4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Create a bucket if you don't have one\n",
        "# You only need to run this once\n",
        "!gsutil mb gs://$BUCKET"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xajH6qhl-KOF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 70
        },
        "outputId": "01a19686-4d67-4f62-82d2-ee2af6f90196"
      },
      "source": [
        "!gsutil cp 'model.bst' gs://$BUCKET"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Copying file://model.bst [Content-Type=application/octet-stream]...\n",
            "/ [1 files][ 67.4 KiB/ 67.4 KiB]                                                \n",
            "Operation completed over 1 objects/67.4 KiB.                                     \n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ocIq4iDU7xfX",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 122
        },
        "outputId": "b00eeef8-69ac-4256-a763-797a9f2dd09b"
      },
      "source": [
        "# Create the model resource\n",
        "!gcloud ai-platform models create flight_delay_prediction"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using endpoint [https://ml.googleapis.com/]\n",
            "\u001b[1;33mWARNING:\u001b[0m Please explicitly specify a region. Using [us-central1] by default on https://ml.googleapis.com. Please note that your model will be inaccessible from https://us-central1-ml.googelapis.com\n",
            "\n",
            "Learn more about regional endpoints and see a list of available regions: https://cloud.google.com/ai-platform/prediction/docs/regional-endpoints\n",
            "Created ml engine model [projects/sara-cloud-ml/models/flight_delay_prediction].\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6VqYBeKI7xh1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "ed7be291-07e2-4266-abd2-b0c05f5f0ad7"
      },
      "source": [
        "# Create the version\n",
        "!gcloud ai-platform versions create 'v1' \\\n",
        "  --model 'flight_delay_prediction' \\\n",
        "  --origin gs://$BUCKET \\\n",
        "  --runtime-version=1.15 \\\n",
        "  --framework 'XGBOOST' \\\n",
        "  --python-version=3.7"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using endpoint [https://ml.googleapis.com/]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8JolsfzZ7xn7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Get a prediction on the first example from our test set\n",
        "!rm input.json\n",
        "num_examples = 10\n",
        "with open('input.json', 'a') as f:\n",
        "  for i in range(num_examples):\n",
        "    f.write(str(x_test.iloc[i].values.tolist()))\n",
        "    f.write('\\n')"
      ],
      "execution_count": 28,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "f4JvY29OA2EV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 214
        },
        "outputId": "0fcd99b7-2ff4-4a4c-a75b-6cc6a4a9ae93"
      },
      "source": [
        "!cat input.json"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[5.0, 59.0, 241.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0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 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VlaMvecP7xqw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 72
        },
        "outputId": "43b8dbf5-13de-48e5-c8aa-2214ef49166e"
      },
      "source": [
        "# Make a prediction to the deployed model\n",
        "!gcloud ai-platform predict --model 'flight_delay_prediction' --version \\\n",
        "  'v1' --json-instances 'input.json'"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using endpoint [https://ml.googleapis.com/]\n",
            "[0.4868972599506378, 0.004682584665715694, 0.004437300376594067, 0.004618476610630751, 0.01615077443420887, 0.0025173116009682417, 0.06883466243743896, 0.0035972497425973415, 0.008341211825609207, 0.01127849891781807]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5VEb1HSXAnIw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 123
        },
        "outputId": "bc6623a4-d839-4d18-a0d2-5ad1c58800cc"
      },
      "source": [
        "# Compare this with actual values\n",
        "print(y_test.iloc[:5])"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "140323    0.0\n",
            "66094     0.0\n",
            "63096     0.0\n",
            "192118    0.0\n",
            "222633    0.0\n",
            "Name: arr_delay_bool, dtype: float64\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TEHfch_7vccv",
        "colab_type": "text"
      },
      "source": [
        "## Model version #2: replace XGBoost with TensorFlow"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rpHlBs1wwbep",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "tf_model = tf.keras.Sequential([\n",
        "    tf.keras.layers.Dense(32, activation='relu', input_shape=[len(x_train.iloc[0])]),\n",
        "    tf.keras.layers.Dense(32, activation='relu'),\n",
        "    tf.keras.layers.Dense(1, activation='sigmoid')                       \n",
        "])\n",
        "\n",
        "tf_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])"
      ],
      "execution_count": 36,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NUR_E3JKxU6G",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 410
        },
        "outputId": "ce0372a6-f365-41c0-8971-b054715dba41"
      },
      "source": [
        "tf_model.fit(x_train, y_train, epochs=10, validation_split=0.1)"
      ],
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch 1/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.2069 - accuracy: 0.9468 - val_loss: 0.1038 - val_accuracy: 0.9635\n",
            "Epoch 2/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.1114 - accuracy: 0.9609 - val_loss: 0.1003 - val_accuracy: 0.9654\n",
            "Epoch 3/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.1042 - accuracy: 0.9630 - val_loss: 0.1198 - val_accuracy: 0.9578\n",
            "Epoch 4/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.1006 - accuracy: 0.9640 - val_loss: 0.1009 - val_accuracy: 0.9638\n",
            "Epoch 5/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.0993 - accuracy: 0.9643 - val_loss: 0.1046 - val_accuracy: 0.9619\n",
            "Epoch 6/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.0971 - accuracy: 0.9654 - val_loss: 0.1015 - val_accuracy: 0.9643\n",
            "Epoch 7/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.0968 - accuracy: 0.9652 - val_loss: 0.0989 - val_accuracy: 0.9654\n",
            "Epoch 8/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.0962 - accuracy: 0.9653 - val_loss: 0.1154 - val_accuracy: 0.9583\n",
            "Epoch 9/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.0956 - accuracy: 0.9659 - val_loss: 0.0987 - val_accuracy: 0.9644\n",
            "Epoch 10/10\n",
            "4847/4847 [==============================] - 8s 2ms/step - loss: 0.0949 - accuracy: 0.9658 - val_loss: 0.0988 - val_accuracy: 0.9658\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tensorflow.python.keras.callbacks.History at 0x7fce33c04978>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "j0w7FMs3xmEN",
        "colab_type": "text"
      },
      "source": [
        "Note that accuracy will be similar to the XGBoost model. We're just using this to demonstrate how training a model with a different framework could be deployed as a new version."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BsfTtsKYxdsD",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "555a124a-43fa-4ebb-dc0f-d6e0db15f011"
      },
      "source": [
        "metrics = tf_model.evaluate(x_test, y_test)\n",
        "print(metrics)"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1795/1795 [==============================] - 2s 1ms/step - loss: 0.0954 - accuracy: 0.9661\n",
            "[0.09541851282119751, 0.9660840034484863]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hI_m94vXyDZZ",
        "colab_type": "text"
      },
      "source": [
        "Next we'll deploy the updated TF model to AI Platform as a v2."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jHh04sjwyS-M",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "tf_model_path = 'gs://' + BUCKET + '/tf'"
      ],
      "execution_count": 48,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LtETMBWVyGQO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "tf_model.save(tf_model_path, save_format='tf')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Z6hR0COQyopa",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "40434f44-59e2-46f2-b23b-10694f6b93cd"
      },
      "source": [
        "!gcloud ai-platform versions create 'v2' \\\n",
        "  --model 'flight_delay_prediction' \\\n",
        "  --origin $tf_model_path \\\n",
        "  --runtime-version=2.1 \\\n",
        "  --framework 'TENSORFLOW' \\\n",
        "  --python-version=3.7"
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using endpoint [https://ml.googleapis.com/]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q3vbIhmhzpsD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Make a prediction to the new version\n",
        "!gcloud ai-platform predict --model 'flight_delay_prediction' --version \\\n",
        "  'v2' --json-instances 'input.json'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GAD71nf-3yBL",
        "colab_type": "text"
      },
      "source": [
        "## Alternative: reframe as a regression problem\n",
        "\n",
        "In this case, you'd likely want to create a new model resource since the response format of your model has changed."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CLJkac5I2k2M",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "regression_model_labels = df['arr_delay']\n",
        "regression_model_data = df.drop(columns=['arr_delay', 'arr_delay_bool'])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9kmsQuFm2Wmf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x,y = regression_model_data,regression_model_labels\n",
        "x_train,x_test,y_train,y_test = train_test_split(x,y)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uc0cA9ux25fc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = xgb.XGBRegressor(\n",
        "    objective='reg:linear'\n",
        ")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wjtQpUkQ2_MT",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# This will take 1-2 minutes to run\n",
        "model.fit(x_train, y_train)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j3AVLX1-3Arv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y_pred = model.predict(x_test)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Dvp2IBDB3DqF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for i,val in enumerate(y_pred[:10]):\n",
        "  print(val)\n",
        "  print(y_test.iloc[i])\n",
        "  print()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TJESyTDC3Q4H",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model.save_model('model.bst')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BlAUcd6G6GSM",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "59b9e8bc-6405-41b1-8ce6-05e75e612bd2"
      },
      "source": [
        "!gsutil cp model.bst gs://$BUCKET/regression/"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Copying file://model.bst [Content-Type=application/octet-stream]...\n",
            "/ [1 files][ 67.8 KiB/ 67.8 KiB]                                                \n",
            "Operation completed over 1 objects/67.8 KiB.                                     \n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5LJ5CLemwwhi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!gcloud ai-platform models create 'flights_regression'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hVPotaI_6I-9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "982bb232-aae0-4b23-e6b6-e45e96c51f87"
      },
      "source": [
        "# Create the version\n",
        "!gcloud ai-platform versions create 'v1' \\\n",
        "  --model 'flights_regression' \\\n",
        "  --origin gs://$BUCKET/regression \\\n",
        "  --runtime-version=1.15 \\\n",
        "  --framework 'XGBOOST' \\\n",
        "  --python-version=3.7"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using endpoint [https://ml.googleapis.com/]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ztlk3JLo78wd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!gcloud ai-platform predict --model 'flighs_regression' --version \\\n",
        "  'v1' --json-instances 'input.json'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0oONy5KtY9vP",
        "colab_type": "text"
      },
      "source": [
        "Copyright 2020 Google Inc. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License"
      ]
    }
  ]
}